package com.example.administrator.szb.activity.myyewu;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.YWGLBean;
import com.example.administrator.szb.util.SizeUtils;
import com.example.administrator.szb.view.TipsPop;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/2/26.
 */

public class YwAdapter1 extends BaseAdapter {
    private Activity context;
    private List<YWGLBean> list;
    LayoutInflater inflater;
    private List<Integer> integerList = new ArrayList<>();
    public YwAdapter1(Activity context, List list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    private List<Integer> integerList2 = new ArrayList<>();
    public void setIntegerList2(List<Integer> l) {
        integerList2.clear();
        integerList2.addAll(l);
    }
    public List<Integer> getIntegerList() {
        return integerList;
    }

    public void setIntegerList(List<Integer> l) {

        integerList.clear();
        integerList.addAll(l);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public  Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    ViewHolder holder;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
          holder=null;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.view_ywgl_no_new_two_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ywgl_no_new_text.setText(list.get(position).getTitle());
        holder.ywgl_no_new_text_gpzy.setVisibility(View.INVISIBLE);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.right_img.getLayoutParams();
        if (list.get(position).getType()==1){
            params.width = SizeUtils.dip2px(context,30);
            params.height = SizeUtils.dip2px(context,30);
            //无箭头
            if (integerList.contains(list.get(position).getId())){
                holder.right_img.setImageResource(R.drawable.choose_check_new);
            }else {
                holder.right_img.setImageResource(R.drawable.unchoose_check_new);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (integerList.contains(list.get(position).getId())){
                        integerList.remove((Integer) list.get(position).getId());
                    }else {

                        integerList.add(list.get(position).getId());
                    }
                    notifyDataSetChanged();
                }
            });
        }else {
            if (integerList2.contains(list.get(position).getId())){
                holder.ywgl_no_new_text_gpzy.setVisibility(View.VISIBLE);
            }else {

                holder.ywgl_no_new_text_gpzy.setVisibility(View.INVISIBLE);
            }
            params.width = SizeUtils.dip2px(context,15);
            params.height = SizeUtils.dip2px(context,15);
            holder.right_img.setImageResource(R.mipmap.more);
            //有箭头
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener!=null){
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        }
        holder.right_img.setLayoutParams(params);
        holder.imageView.setVisibility(View.GONE);
        final String strs = list.get(position).getBusiness_desc();
        if (!TextUtils.isEmpty(strs)){
            holder.imageView.setVisibility(View.VISIBLE);
        }

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TipsPop.show(context,holder.imageView,strs);
            }
        });
        return convertView;
    }
    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    interface OnItemClickListener{
        void onItemClick(int pos);
    }


    class ViewHolder{
         LinearLayout ywgl_no_new_ll_gpzy;
         TextView ywgl_no_new_text;
         LinearLayout wenhao;
         TextView ywgl_no_new_text_gpzy;
        ImageView right_img;
        LinearLayout imageView;
        public ViewHolder(View view){
             imageView = (LinearLayout)view.findViewById(R.id.wenhao);
            ywgl_no_new_ll_gpzy = (LinearLayout) view.findViewById(R.id.ywgl_no_new_ll_gpzy);
            ywgl_no_new_text = (TextView) view.findViewById(R.id.ywgl_no_new_text);
            wenhao = (LinearLayout) view.findViewById(R.id.wenhao);
            ywgl_no_new_text_gpzy = (TextView) view.findViewById(R.id.ywgl_no_new_text_gpzy);
            right_img  = (ImageView) view.findViewById(R.id.right_img);
        }
    }
}